using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using QFramework.Example;
using UnityEngine;

namespace QFramework
{
    public class ItemKit
    {
	    /// <summary>
	    /// 存档&加载
	    /// </summary>
	    public static IItemKitSaverAndLoader SaverAndLoader = new DefaultItemKitSaverAndLoader();

	    /// <summary>
	    /// 资源加载器
	    /// </summary>
	    public static IItemKitLoader Loader = new DefaultItemKitLoader();
	    
	    /// <summary>
	    /// 当前点击到的格子
	    /// </summary>
	    public static UISlot CurrentSlotPointerOn = null;

	    /// <summary>
	    /// 背包格子组集合，所有的格子组都存在这里
	    /// </summary>
	    public static Dictionary<string, SlotGroup> mSlotGroupByKey = new Dictionary<string, SlotGroup>();
	    
	    /// <summary>
	    /// 创建格子组
	    /// </summary>
	    /// <param name="key"></param>
	    /// <returns></returns>
	    public static SlotGroup CreateSlotGroup(string key)
	    {
		    var slotGroup = new SlotGroup()
		    {
			    Key = key,
		    };
		    mSlotGroupByKey.Add(key,slotGroup);
		    return slotGroup;
	    }

	    /// <summary>
	    /// 根据key获得格子组
	    /// </summary>
	    /// <param name="key"></param>
	    /// <returns></returns>
	    public static SlotGroup GetSlotGroupByKey(string key)=>mSlotGroupByKey[key];
	    
	    /// <summary>
	    /// 获取道具配置
	    /// </summary>
	    /// <param name="itemKey"></param>
	    /// <returns></returns>
	    public static IItem GetItemConfig(string itemKey)
	    {
		    return ItemByKey[itemKey];
	    }

	    public const string DefaultLanguage = "DefaultLanguage";
	    public static string CurrentLanguage { get; private set; } = DefaultLanguage;
	    
	    /// <summary>
	    /// 加载道具配置
	    /// </summary>
	    /// <param name="databaseName"></param>
	    public static void LoadItemDatabase(string databaseName)
	    {
		    //var database = Resources.Load<ItemDatabase>(databaseName);
		    var database=Loader.LoadDatabase(databaseName);
		    foreach (var databaseItem in database.Items)
		    {
			    AddItemConfig(databaseItem);
		    }
	    }
	    
	    /// <summary>
	    /// 异步加载道具配置
	    /// </summary>
	    /// <param name="databaseName"></param>
	    public static IEnumerator LoadItemDatabaseAsync(string databaseName)
	    {
		    bool done = false;
		    Loader.LoadItemDatabaseAsync(databaseName,(database =>
		    {
			    foreach (var databaseItem in database.Items)
			    {
				    AddItemConfig(databaseItem);
			    }

			    done = true;
		    }));

		    while (!done)
		    {
			    yield return null;
		    }
	    }
	    
	    /// <summary>
	    /// 加载语言包
	    /// </summary>
	    /// <param name="languagePackageName"></param>
	    public static void LoadLanguagePackage(string languagePackageName)
	    {
		    if (languagePackageName==DefaultLanguage)
		    {
			    CurrentLanguage = DefaultLanguage;

			    foreach (var item in ItemByKey.Values)
			    {
				    item.LocaleItem = null;
			    }
		    }
		    else
		    {
			    CurrentLanguage= languagePackageName;
			    //var languagePackage = Resources.Load<ItemLanguagePackage>(languagePackageName);
			    var languagePackage =Loader.LoadLanguagePackage(languagePackageName);
			    foreach (var localeItem in languagePackage.LocaleItems)
			    {
				    if (ItemByKey.TryGetValue(localeItem.Key,out var item))
				    {
					    item.LocaleItem = localeItem;
				    }
			    }
		    }
	    }
	    
	    /// <summary>
	    /// 异步加载语言包
	    /// </summary>
	    /// <param name="languagePackageName"></param>
	    public static IEnumerator LoadLanguagePackageAsync(string languagePackageName)
	    {
		    bool done = false;
		    if (languagePackageName==DefaultLanguage)
		    {
			    CurrentLanguage = DefaultLanguage;

			    foreach (var item in ItemByKey.Values)
			    {
				    item.LocaleItem = null;
			    }
			    done = true;
		    }
		    else
		    {
			    CurrentLanguage= languagePackageName;
			    //var languagePackage = Resources.Load<ItemLanguagePackage>(languagePackageName);
			    Loader.LoadItemLanguagePackageAsync(languagePackageName, languagePackage =>
			    {
				    foreach (var localeItem in languagePackage.LocaleItems)
				    {
					    if (ItemByKey.TryGetValue(localeItem.Key,out var item))
					    {
						    item.LocaleItem = localeItem;
					    }
				    }
				    done = true;
			    });
		    }

		    while (!done)
		    {
			    yield return null;
		    }
	    }
	    
	    /// <summary>
	    /// 增加道具配置
	    /// </summary>
	    /// <param name="itemConfig"></param>
	    public static void AddItemConfig(IItem itemConfig)
	    {
		    //Debug.Log(itemConfig);
		    ItemByKey.Add(itemConfig.GetKey,itemConfig);
	    }

        public static Dictionary<string, IItem> ItemByKey = new Dictionary<string, IItem>();

        public static void Save() => SaverAndLoader.Save(mSlotGroupByKey);

        public static void Load() => SaverAndLoader.Load(mSlotGroupByKey);

        public static void Clear() => SaverAndLoader.clear();
    }
}
